﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    class Setting_AddActionTriggerViewModel : CCommandViewModel
    {
        Trigger _Data = null;
        List<KeyValueAssignment> _EventSourceItems = null;
        List<ConditionItem> _ConditionItems = null;
        ConditionItem _SelectedCondition = null;
        string _SelectedEvent = "";
        string _ElementType = "";

        public Setting_AddActionTriggerViewModel(string ElementName, string ElementType)
        {
            base.ViewName = LanguageStrings.GetText("Add new Trigger");

            _Data = new Trigger();

            if (ElementType.ToUpper() == "CONTEXTMENU")
            {
                EventSourceItems = Meta_Elements.GetTypeEvents(ElementType);
            }
            else
            {
                EventSourceItems = Meta_Elements.GetGUIElementEvents(ElementName);
            }

            _ConditionItems = new List<ConditionItem>();
            _ElementType = ElementType;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_ExecAddCondition())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_ExecEditCondition())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_ExecDeleteCondition())));
        }

        public Setting_AddActionTriggerViewModel(Trigger Data, string ElementName, string ElementType)
        {
            base.ViewName = LanguageStrings.GetText("Edit Trigger");

            _Data = Data;

            if (ElementType.ToUpper() == "CONTEXTMENU")
            {
                EventSourceItems = Meta_Elements.GetTypeEvents(ElementType);
            }
            else
            {
                EventSourceItems = Meta_Elements.GetGUIElementEvents(ElementName);
            }

            _ConditionItems = _Data.Conditions;// new List<ConditionItem>();
            _SelectedEvent = _Data.Event.ToString();
            _ElementType = ElementType;

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_ExecAddCondition())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_ExecEditCondition())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_ExecDeleteCondition())));
        }

        #region Properties

        public CSingleCommandViewModel Cmd_Ok
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Close
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_AddCondition
        {
            get { return m_oCommands[2]; }
        }

        public CSingleCommandViewModel Cmd_EditCondition
        {
            get { return m_oCommands[3]; }
        }

        public CSingleCommandViewModel Cmd_DeleteCondition
        {
            get { return m_oCommands[4]; }
        }

        public Trigger Data
        {
            get { return _Data; }
        }

        public List<KeyValueAssignment> EventSourceItems
        {
            get { return _EventSourceItems; }
            set
            {
                _EventSourceItems = value;
                OnPropertyChanged("EventSourceItems");
            }
        }

        public List<ConditionItem> ConditionItems
        {
            get { return _ConditionItems; }
            set
            {
                _ConditionItems = value;
                OnPropertyChanged("ConditionItems");
            }
        }

        public string SelectedEvent
        {
            get { return _SelectedEvent; }
            set
            {
                _SelectedEvent = value;
                OnPropertyChanged("SelectedEvent");
            }
        }

        public ConditionItem SelectedCondition
        {
            get { return _SelectedCondition; }
            set
            {
                _SelectedCondition = value;
                OnPropertyChanged("SelectedCondition");
            }
        }

        #endregion

        #region Command Procedures

        void CMd_DiscardChanges()
        {
            try
            {
                Setting_AddActionTrigger parentDlg = null;
                if ((Setting_AddActionTrigger)((CDelegatingCommand)Cmd_Close.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionTrigger)((CDelegatingCommand)Cmd_Close.Command).CommandParameter;

                    if (parentDlg != null)
                        parentDlg.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText(ErrorMessages.USERSETTINGS__ERROR_WHILE_DISCARD_CHANGES), 
                                             Ex, EMessageTypes.Error);
            }
        }

        void CMd_OkDialog()
        {
            try
            {
                Setting_AddActionTrigger parentDlg = null;
                if ((Setting_AddActionTrigger)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionTrigger)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter;

                    if (parentDlg != null)
                    {
                        if (_SelectedEvent != "")
                        {
                            _Data.Event = (eTriggerEvent)Enum.Parse(typeof(eTriggerEvent), _SelectedEvent);
                            _Data.Conditions = ConditionItems;
                            //_Data.Property = SelectedPropertyName;
                            parentDlg.DialogResult = true;
                            parentDlg.Close();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_APPLY_CHANGES), 
                                              Ex, EMessageTypes.Error);
            }
        }

        void Cmd_ExecAddCondition()
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddCondition.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddCondition.Command).CommandParameter;
                    Setting_AddActionConditionViewModel tmpViewModel = new Setting_AddActionConditionViewModel();
                    Setting_AddActionCondition tmpDialog = new Setting_AddActionCondition();
                    tmpDialog.DataContext = tmpViewModel;
                    if (tmpDialog.ShowDialog() == true)
                    {
                        ConditionItem tmpCondition = ((Setting_AddActionConditionViewModel)tmpDialog.DataContext).Data;
                        ConditionItems.Add(tmpCondition);
                        tmpListView.Items.Refresh();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText(ErrorMessages.USERSETTINGS__ERROR_ADD_ACTION_CONDITION_FAILED),
                                              Ex, EMessageTypes.Error);
            }
        }

        void Cmd_ExecEditCondition() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditCondition.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditCondition.Command).CommandParameter;
                    Setting_AddActionConditionViewModel tmpViewModel = new Setting_AddActionConditionViewModel(_SelectedCondition);
                    Setting_AddActionCondition tmpDialog = new Setting_AddActionCondition();
                    tmpDialog.DataContext = tmpViewModel;
                    if (tmpDialog.ShowDialog() == true)
                    {
                        ConditionItem tmpCondition = ((Setting_AddActionConditionViewModel)tmpDialog.DataContext).Data;
                        _SelectedCondition = tmpCondition;                        
                        tmpListView.Items.Refresh();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText(ErrorMessages.USERSETTINGS__ERROR_EDIT_ACTION_CONDITION_FAILED),
                                              Ex, EMessageTypes.Error);
            }
        }

        void Cmd_ExecDeleteCondition() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditCondition.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditCondition.Command).CommandParameter;

                    if (_SelectedCondition != null)
                    {
                        if (System.Windows.MessageBox.Show(LanguageStrings.GetText("Delete selected condition?", EStringTypes.stQuestion), 
                            LanguageStrings.GetText("Question"), System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            ConditionItems.Remove(_SelectedCondition);
                            OnPropertyChanged("ConditionItems");
                            tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText(ErrorMessages.USERSETTINGS__ERROR_DELETE_ACTION_CONDITION),
                                              Ex, EMessageTypes.Error);
            }
        }

        #endregion
    }
}
